/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ddd.service;

import com.ddd.context.Parameters;
import com.ddd.entity.Publisher;
import com.ddd.util.HibernateUtil;
import com.ddd.util.Util;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class PublisherService extends BaseService {

    private Parameters para;
    private LogService logService;

    public PublisherService(HttpServletRequest request) {
        para = new Parameters(request);
        logService = new LogService(request);
    }

    public Publisher load(String id) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();

        Publisher publisher = (Publisher) session.load(Publisher.class, id);
        //Hibernate.initialize(Publisher);

        tx.commit();
        session.close();

        return publisher;
    }

    public ArrayList loadAll() {
        ArrayList result = new ArrayList();
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Publisher.class);
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            result.add(iter.next());
        }
        tx.commit();
        session.close();
        para.setTotalSize(result.size());
        return result;
    }

    public void save(Publisher publisher) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        publisher.setCid(para.getUsername());
        publisher.setCtime(Util.getTodayWithTime());
        session.save(publisher);
        tx.commit();
        session.close();
    }

    public void update(Publisher publisher) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        publisher.setUid(para.getUsername());
        publisher.setUtime(Util.getTodayWithTime());
        session.update(publisher);
        tx.commit();
        session.close();
    }

    public void delete(Publisher publisher) {
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        session.delete(publisher);
        tx.commit();
        session.close();
    }

    public Publisher loadByName(String name) {
        logger.debug("name = " + name);
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(Publisher.class);
        criteria.add(Restrictions.eq("name" , name));
        Iterator iter = criteria.list().iterator();
        
        Publisher result = null; 
        while (iter.hasNext()) {
            result = (Publisher)iter.next();
            break;
        }
        tx.commit();
        session.close();
        //para.setTotalSize(result.size());
        return result;
    }
}
